package com.kit.data.kitopeso.activity;

import android.content.Intent;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.viewbinding.ViewBinding;

import com.google.gson.JsonObject;
import com.kit.data.kitopeso.KitoApp;
import com.kit.data.kitopeso.PathMaping;
import com.kit.data.kitopeso.R;
import com.kit.data.kitopeso.adapter.CustomerServiceAdapter;
import com.kit.data.kitopeso.base.BaseActivity;
import com.kit.data.kitopeso.base.BaseParams;
import com.kit.data.kitopeso.databinding.KitoCustomerserviceActivityBinding;
import com.kit.data.kitopeso.net.PU;
import com.kit.data.kitopeso.net.RxHttp;
import com.kit.data.kitopeso.net.U;
import com.kit.data.kitopeso.obj.KitoAppInfo;
import com.kit.data.kitopeso.obj.KitoCustomerService;
import com.kit.data.kitopeso.tools.AESCryptUtilJar;
import com.kit.data.kitopeso.tools.JsonParse;
import com.kit.data.kitopeso.tools.LogUtil;
import com.kit.data.kitopeso.tools.ResUtils;
import com.kit.data.kitopeso.tools.SpUtils;
import com.kit.data.kitopeso.tools.ViewUtils;
import com.kit.data.kitopeso.view.KitoSpaceItemDecoration;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;

public class KitoCustomerServiceActivity extends BaseActivity<KitoCustomerserviceActivityBinding> {


    CustomerServiceAdapter adater;
    List<KitoCustomerService> dataList;

    String token;
    KitoAppInfo appInfo;


    @Override
    protected KitoCustomerserviceActivityBinding initViewBinding(LayoutInflater inflater) {
        return KitoCustomerserviceActivityBinding.inflate(inflater);
    }

    @Override
    protected void initView() {

        token = getIntent().getStringExtra("token");
        String date = ResUtils.s(R.string.cs_date_subtitle);
        if (KitoApp.getInstance().getAppInfo() != null && !TextUtils.isEmpty(KitoApp.getInstance().getAppInfo().getCustomerServiceDate())) {
            date = KitoApp.getInstance().getAppInfo().getCustomerServiceDate();
        }
        binding.tvDate.setText(Html.fromHtml(ResUtils.s(R.string.cs_date_title, date)));
        binding.tvColse.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            finish();
        });
        binding.tvFb.setOnClickListener(v -> {
            ViewUtils.viewScale(v);
            finish();
        });
        dataList = new ArrayList<>();
        adater = new CustomerServiceAdapter(dataList);
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.recyclerView.setLayoutManager(manager);
        int d = ResUtils.d(R.dimen.dp_8);
        binding.recyclerView.addItemDecoration(new KitoSpaceItemDecoration(d, d, getActivity()));
        adater.setOnBtnItemClickListener(position -> {
            KitoCustomerService item = dataList.get(position);
            if (item.isPhone()) {
                PU.userBuriedPoint(PU.P63, "");
                toCall(item.getData());
            } else {
            }
        });
        binding.recyclerView.setAdapter(adater);

    }

    @Override
    protected void initData() {
        UP_TYPE1 = PU.P61;
        UP_TYPE2 = PU.P62;
        appInfo = KitoApp.getInstance().getAppInfo();
        loadData();
        getAppInfo();
    }

    void loadData() {
        if (getActivity() != null && !isFinishing()) {
            if (appInfo != null) {
                dataList.clear();
                String mobile = appInfo.getMobile();
                String email = appInfo.getEmail();
                if (!TextUtils.isEmpty(mobile)) {
                    if (mobile.contains(",")) {
                        String[] mobileList = mobile.split(",");
                        for (String s : mobileList) {
                            KitoCustomerService bean = new KitoCustomerService();
                            bean.setPhone(true);
                            bean.setData(s);
                            dataList.add(bean);
                        }
                    } else {
                        KitoCustomerService bean = new KitoCustomerService();
                        bean.setPhone(true);
                        bean.setData(mobile);
                        dataList.add(bean);
                    }
                }
                if (!TextUtils.isEmpty(email)) {
                    if (email.contains(",")) {
                        String[] emailList = email.split(",");
                        for (String s : emailList) {
                            KitoCustomerService bean = new KitoCustomerService();
                            bean.setPhone(false);
                            bean.setData(s);
                            dataList.add(bean);
                        }
                    } else {
                        KitoCustomerService bean = new KitoCustomerService();
                        bean.setPhone(false);
                        bean.setData(email);
                        dataList.add(bean);
                    }
                }
                adater.updateAllData();
            }
        }
    }

    void toCall(String phoneNumber) {
        Intent dialIntent = new Intent(Intent.ACTION_DIAL);
        dialIntent.setData(Uri.parse("tel:" + phoneNumber));
        startActivity(dialIntent);
    }

    void getAppInfo() {
        String path = PathMaping.PathList[0];
        JsonObject json;
        LogUtil.e("me", SpUtils.get_string(this, SpUtils.KEY_TOKEN, "") + "<-token->" + token);
        HashMap<String, String> heads = new HashMap<>();
        if (!TextUtils.isEmpty(token)) {
            json = BaseParams.getJsonObject(getActivity(), path, U.URL_MAIN_PRODUCT, token);
            heads.put("product", U.URL_MAIN_PRODUCT);
        } else {
            json = BaseParams.getJsonObject(getActivity(), path);
            heads.put("product", U.URL_PRODUCT);
        }
        if (!TextUtils.isEmpty(U.URL_COUNTRY)) {
            heads.put("country", U.URL_COUNTRY);
        }
        String data = AESCryptUtilJar.encryptDefault(json.toString());
        LogUtil.e("me", json + "--data--" + data);
        RxHttp.post(U.commonUrl, data, heads)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                    }

                    @Override
                    public void onNext(String response) {
                        if (!TextUtils.isEmpty(response)) {
                            String rjson = AESCryptUtilJar.decryptDefault(response);
                            LogUtil.e("me", "getAppInfo json->" + rjson);
                            if (JsonParse.getCode(rjson)) {
                                SpUtils.save_string(getActivity(), SpUtils.KEY_APP_INFO, rjson);
                                appInfo = JsonParse.appInfoJson2Obj(rjson);
                                loadData();
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onComplete() {
                    }
                });
    }


}
